//
//  CS_Page_CellButton.m
//  Pods
//
//  Created by 浙大网新中研软件 on 2017/9/25.
//
//

#import "CS_Page_CellButton.h"

#define ARROW_IMAGE_MARGN 4.0
#define ARROW_IMAGE_WIDTH 25.0
#define MIN_CELL_HEIGHT 30.0

@interface CS_Page_CellButton()

@property (nonatomic, strong) id cellTag;

@property (nonatomic, copy) CellButtonLayoutSubViewBlock layoutSubViewHandler;

@property (nonatomic, copy) CellButtonClickHandler clickHandler;

@property (nonatomic, strong) UIImageView *arrowImageView;

@property (nonatomic, strong) UIButton *cellButton;

@end

@implementation CS_Page_CellButton

+(id)page_CellButtonWithCellTag:(id)cellTag layoutSubViewHandler:(CellButtonLayoutSubViewBlock)layoutSubViewHandler clickHander:(CellButtonClickHandler)clickHandler{
    
    CS_Page_CellButton *page_CellButton = [CS_Page_CellButton new];
    page_CellButton.cellTag = cellTag;
    page_CellButton.layoutSubViewHandler = layoutSubViewHandler;
    page_CellButton.clickHandler = clickHandler;
    
    return page_CellButton;
}

-(CGFloat)layoutView:(UIView *)superView primaryY:(CGFloat)primaryY{
    
    CGFloat localPrimaryY = 0.0;
    
    if (self.layoutSubViewHandler) {
        
        localPrimaryY = self.layoutSubViewHandler(self, self.leftMargin + ARROW_IMAGE_MARGN + ARROW_IMAGE_WIDTH);
        
        if (localPrimaryY != 0.0) {
            
            if (localPrimaryY <= MIN_CELL_HEIGHT) {
                
                localPrimaryY = MIN_CELL_HEIGHT;
                
            }
            
            CGFloat imagePrimaryY = (localPrimaryY - ARROW_IMAGE_WIDTH)/ 2.0;
            
            if (self.isSeperatorLineRequired) {
                imagePrimaryY = (localPrimaryY - 10 - ARROW_IMAGE_WIDTH)/ 2.0 + 10;
            }
            
            self.arrowImageView.frame = CGRectMake(kWindowW - 20.0 - 5 - self.rightMargin, imagePrimaryY, 18.0, 18.0);
            
            [self addSubview:self.arrowImageView];
            
            if (self.clickHandler) {
                
                self.cellButton.frame = CGRectMake(0, self.isSeperatorLineRequired ? 10 : 0, kWindowW, self.isSeperatorLineRequired ? localPrimaryY - 10: localPrimaryY);
                
                [self addSubview:self.cellButton];
                
            }
            
            self.frame = CGRectMake(0, primaryY, kWindowW, localPrimaryY);
        }
        
        [superView addSubview:self];
    }
    
    return primaryY + localPrimaryY;
    
}

-(void)cellClick{
    if (self.clickHandler) {
        self.clickHandler(self.cellTag);
    }
}

-(UIImageView *)arrowImageView{
    if (_arrowImageView == nil) {
        UIImage *arrow_more_image = [CSPageEngine getImageWithKey:KEY_ICON_ARROW_MORE];
        _arrowImageView = [[UIImageView alloc] initWithImage:arrow_more_image];
    }
    return _arrowImageView;
}

-(UIButton *)cellButton{
    if (_cellButton == nil) {
        _cellButton = [[UIButton alloc] init];
        [_cellButton addTarget:self action:@selector(cellClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cellButton;
}

@end
